{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "adfdb2a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas\n",
    "import matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a2a40d8",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46bb3a28",
   "metadata": {},
   "source": [
    "- abs 绝对值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38fbb699",
   "metadata": {},
   "source": [
    "- sqrt 平方根"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "831342b6",
   "metadata": {},
   "source": [
    "- square 平方"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "754d704b",
   "metadata": {},
   "source": [
    "- exp 以e为底的指数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3eaa3cdf",
   "metadata": {},
   "source": [
    "- log 自然对数，以e为底的对数，例：ln3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65a0d109",
   "metadata": {},
   "source": [
    "- sin 正弦函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4d9bade",
   "metadata": {},
   "source": [
    "- cos 余弦函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da77c5fb",
   "metadata": {},
   "source": [
    "- tan 正切函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05f596eb",
   "metadata": {},
   "source": [
    "- round 四舍五入，整数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bb24564",
   "metadata": {},
   "source": [
    "- ceil 向上取整"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d861641",
   "metadata": {},
   "source": [
    "- floor 向下取整"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaf3362b",
   "metadata": {},
   "source": [
    "- cumsum 累加求和"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "122a9f1e",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc853625",
   "metadata": {},
   "source": [
    "# Ndarray排序 \n",
    "**注意**：内部使用的是快速排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b69c604b",
   "metadata": {},
   "source": [
    "- numpy.sort() 不改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "37bc604e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 3, 3, 2, 5, 1, 7, 3])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randint(0,10,size=9)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6dcd7ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 3, 3, 2, 5, 1, 7, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 3, 3, 5, 7, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n2 = numpy.sort(n)\n",
    "display(n,n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2e38fdd",
   "metadata": {},
   "source": [
    "- ndarray.sort() 改变原数组，不多占用内存空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "73cc5395",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 3, 3, 5, 7, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n.sort()\n",
    "display(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21637b3d",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb907fc",
   "metadata": {},
   "source": [
    "# 文件操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d5588a7",
   "metadata": {},
   "source": [
    "- save 保存ndarray到一个npy文件\n",
    "- savez 将多个array保存到一个npy文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a1193a40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6, 0, 8, 2, 4, 9, 1, 7, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([6, 8, 8, 1, 9, 0, 6, 2, 2, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n1 = numpy.random.randint(0,10,size=10)\n",
    "n2 = numpy.random.randint(0,10,size=10)\n",
    "display(n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "21f03611",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存数组\n",
    "numpy.save('n1',n1) # 保存的是npy文件\n",
    "\n",
    "# 存多个\n",
    "numpy.savez('arr.npz',xarr=n1,yarr=n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a96f092",
   "metadata": {},
   "source": [
    "- 读取数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2c7018c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6, 0, 8, 2, 4, 9, 1, 7, 9])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取npy文件\n",
    "numpy.load('n1.npy')\n",
    "\n",
    "# 读取npz文件\n",
    "numpy.load('arr.npz')['xarr']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "763d46d7",
   "metadata": {},
   "source": [
    "- csv、txt文件的读取操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f2431327",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 6, 6],\n",
       "       [8, 1, 7, 2],\n",
       "       [2, 2, 8, 8]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randint(0,10,size=(3,4))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b2878d5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 存储到csv或者txt\n",
    "# delimiter=',' 分隔符\n",
    "numpy.savetxt('arr.csv',n,delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "55d16216",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_8520\\2889736509.py:1: DeprecationWarning: loadtxt(): Parsing an integer via a float is deprecated.  To avoid this warning, you can:\n",
      "    * make sure the original data is stored as integers.\n",
      "    * use the `converters=` keyword argument.  If you only use\n",
      "      NumPy 1.23 or later, `converters=float` will normally work.\n",
      "    * Use `np.loadtxt(...).astype(np.int64)` parsing the file as\n",
      "      floating point and then convert it.  (On all NumPy versions.)\n",
      "  (Deprecated NumPy 1.23)\n",
      "  numpy.loadtxt('arr.csv',delimiter=',',dtype=numpy.int16)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 6, 6],\n",
       "       [8, 1, 7, 2],\n",
       "       [2, 2, 8, 8]], dtype=int16)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.loadtxt('arr.csv',delimiter=',',dtype=numpy.int16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8779ee04",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e392750d",
   "metadata": {},
   "source": [
    "# 矩阵乘积"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c77d840",
   "metadata": {},
   "source": [
    "- numpy.dot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
